package com.hecore.lagou.speak.entity;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * @Author: hecore
 * @Date: 2020/10/18 23:52
 * @Description:
 */

public class SpeakBeanSort implements WritableComparable<SpeakBeanSort> {

    // 1.1 custom compare rule
// 重写compareTo⽅法，此处是⽐较⼀个字段如果⽐较2个字段就是⼆次排序
    public int compareTo(SpeakBeanSort o) {
        int result;
        // 按照总流量⼤⼩，倒序排列
        if (sumDuration > o.getSumDuration()) {
            result = 1;
        }else if (sumDuration < o.getSumDuration()) {
            result = -1;
        }else {
            result = 0;
        }
        return result;
    }
    // 1.2 序列化⽅法:就是把内容输出到⽹络或者⽂本中
    public void write(DataOutput output) throws IOException {
        output.writeLong(selfDuration);
        output.writeLong(thirdPartDuration);
        output.writeUTF(deviceId);
        output.writeLong(sumDuration);
    }

    // 1.3
    //反序列化⽅法
    public void readFields(DataInput in) throws IOException {
        this.selfDuration = in.readLong();//⾃由时⻓
        this.thirdPartDuration = in.readLong();//第三⽅时⻓
        this.deviceId = in.readUTF();//设备id
        this.sumDuration = in.readLong();//总时⻓
    }

    //定义属性
    private Long selfDuration;//⾃有内容时⻓
    private Long thirdPartDuration;//第三⽅内容时⻓
    private String deviceId;//设备id
    private Long sumDuration;//总时⻓
    //准备⼀个空参构造
    public SpeakBeanSort() {
    }

    //有参构造
    public SpeakBeanSort(Long selfDuration, Long thirdPartDuration, String
            deviceId) {
        this.selfDuration = selfDuration;
        this.thirdPartDuration = thirdPartDuration;
        this.deviceId = deviceId;
        this.sumDuration = this.selfDuration + this.thirdPartDuration;
    }

    public Long getSelfDuration() {
        return selfDuration;
    }

    public void setSelfDuration(Long selfDuration) {
        this.selfDuration = selfDuration;
    }

    public Long getThirdPartDuration() {
        return thirdPartDuration;
    }

    public void setThirdPartDuration(Long thirdPartDuration) {
        this.thirdPartDuration = thirdPartDuration;
    }

    public String getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }

    public Long getSumDuration() {
        return sumDuration;
    }

    public void setSumDuration(Long sumDuration) {
        this.sumDuration = sumDuration;
    }


}
